package com.airena.controller;

import com.airena.entity.dto.UserRegisterDTO;
import com.airena.entity.po.User;
import com.airena.result.Result;
import com.airena.service.IUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 注册接口
 */
@RestController
@Slf4j
@Tag(name = "用户注册接口")
public class RegisterController {
    
    @Autowired
    private IUserService userService;

    /**
     * 用户注册接口
     * @param userRegisterDTO 用户注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    @ResponseBody
    public Result<String> register(@RequestBody(required = false) UserRegisterDTO userRegisterDTO) {
        log.info("收到注册请求: {}", userRegisterDTO);
        
        // 检查请求体是否为空
        if (userRegisterDTO == null) {
            log.warn("注册请求体为空");
            return Result.error("请求数据不能为空");
        }
        
        try {
            User user = userService.register(userRegisterDTO);
            log.info("用户注册成功：{}", user.getUsername());
            return Result.success("注册成功");
        } catch (Exception e) {
            log.error("用户注册失败：{}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
}